from sqlalchemy.orm import Session, joinedload
from models.models import LearnPageRecord
from schemas.record import PageRecordCreate
from datetime import datetime, timedelta
from sqlalchemy import func

def create_knowledge_point(db: Session, knowledge_point: PageRecordCreate, user_id: int):
    db_point = LearnPageRecord(**knowledge_point.dict(), user_id=user_id)
    db.add(db_point)
    db.commit()
    db.refresh(db_point)
    return db_point

def update_knowledge_point(db: Session, point_id: int, knowledge_point: PageRecordCreate, user_id: int):
    db_point = db.query(LearnPageRecord).filter(LearnPageRecord.id == point_id,LearnPageRecord.user_id == user_id).first()
    if db_point:
        for key, value in knowledge_point.dict().items():
            setattr(db_point, key, value)
        db.commit()
        db.refresh(db_point)
    return db_point

def get_knowledge_point(db: Session, point_id: int):
    return db.query(LearnPageRecord).filter(LearnPageRecord.id == point_id,LearnPageRecord.stone == False).first()

def get_all_knowledge_points(db: Session,user_id:int):
    return db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(LearnPageRecord.user_id == user_id,LearnPageRecord.stone == False).all()

def get_all_knowledge_points_by_material(db: Session, user_id:int, material_id:int):
    return db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(LearnPageRecord.user_id == user_id,LearnPageRecord.stone == True, LearnPageRecord.material_id == material_id ).all()

def get_all_knowledge_points_by_page(db: Session,material_id:int,user_id:int,page_number:int):
    return db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.page_number == page_number).all()

def get_all_knowledge_points_by_page_and_stone(db: Session,material_id:int,user_id:int,page_number:int, stone:bool = False):
    return db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.page_number == page_number,LearnPageRecord.stone == stone).all()

# 获取当日的所有阅读记录,并预加载关联的 learn_material 表的 title 属性
def get_today_knowledge_points(db: Session,user_id:int):
    return db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(LearnPageRecord.user_id == user_id,func.DATE(LearnPageRecord.created_at) == datetime.now().date()).all()

def get_today_knowledge_points_by_stone(db: Session,user_id:int):
    return db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(LearnPageRecord.user_id == user_id,func.DATE(LearnPageRecord.created_at) == datetime.now().date(),LearnPageRecord.stone == False).all()

# 获取指定材料的指定相对日期范围内的所有阅读记录,并预加载关联的 learn_material 表的 title 属性
def get_all_knowledge_points_by_date_range(db: Session, user_id: int, start_date: int, end_date: int, material_id: int):
    today = datetime.now().date()
    if start_date == 0:
        start = today
    else:
        start = today - timedelta(days=start_date)
        
    end = today - timedelta(days=end_date)
    print(f"start: {start}, end: {end}")
        
    query = db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(
        LearnPageRecord.user_id == user_id,
        func.date(LearnPageRecord.created_at) <= start,
        func.date(LearnPageRecord.created_at) >= end, 
        LearnPageRecord.material_id == material_id,
    )
    return query.all()


def remove_all_knowledge_points_by_page(db: Session,material_id:int,user_id:int,page_number:int, stone:bool = False):
    if stone:
        res = db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.page_number == page_number,LearnPageRecord.stone == stone).delete()
    else:
        res = db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.page_number == page_number).delete()
    db.commit()
    return res


def delete_knowledge_point(db: Session, point_id: int,user_id:int):
    knowledge_point = db.query(LearnPageRecord).filter(LearnPageRecord.id == point_id,LearnPageRecord.user_id == user_id).first()
    if knowledge_point:
        db.delete(knowledge_point)
        db.commit()
        return True
    return False

## 查询指定材料中是否有未用于生成思维导图的阅读记录
def get_material_mind_map_status(db: Session,material_id:int,user_id:int):
    return db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.mind_map_status == False).all()

## 更新指定材料中所有阅读记录用于生成思维导图
def update_material_mind_map_status(db: Session,material_id:int,user_id:int):
    db.query(LearnPageRecord).filter(LearnPageRecord.material_id == material_id,LearnPageRecord.user_id == user_id,LearnPageRecord.mind_map_status == False).update({"mind_map_status": True})
    db.commit()
    return True

## 查询指定材料中是否有未用于生成知识卡片的阅读记录
def get_material_card_status(db: Session,user_id:int):
    return db.query(LearnPageRecord).options(joinedload(LearnPageRecord.material)).filter(LearnPageRecord.user_id == user_id,LearnPageRecord.card_status == False).all()

## 更新指定材料中所有阅读记录用于生成知识卡片
def update_material_card_status(db: Session, material_id: int, page_number_list: list , user_id: int):
    query = db.query(LearnPageRecord).filter(
        LearnPageRecord.material_id == material_id,
        LearnPageRecord.user_id == user_id,
        LearnPageRecord.card_status == False
    )
    
    if page_number_list:
        query = query.filter(LearnPageRecord.page_number.in_(page_number_list))
        
    query.update({"card_status": True})
    db.commit()
    return True